<?php

/**
 *  调课
 */

class Controller_absent extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->syllabus = YOU::loadModel('syllabus');
		$this->model->grade    = YOU::loadModel('grade');
		$this->model->class    = YOU::loadModel('class');
		$this->model->subject  = YOU::loadModel('subject');
		$this->model->staff    = YOU::loadModel('staff');
		$this->model->semester = YOU::loadModel('semester');
		$this->model->log      = YOU::loadModel('log');

	}


	/** 首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}


	/** 添加调代课 同时写入name**/
	public function actionAdd()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		if ($_POST){
			$class_id = $this->post->class_id;
			$from_subject   = $this->post->from_subject;
			$to_subject     = $this->post->to_subject;

			$start_time     = $this->post->start_time;
			$end_time       = $this->post->end_time;
			$absent_section = $this->post->absent_section;
			$from_staff     = $this->post->from_staff;
			$to_staff       = $this->post->to_staff;
			$absent_item    = $this->post->absent_item;
			$absent_note    = $this->post->absent_note;


			if (empty($absent_item)){
				Helper_Client::alert("请选择请假原因");
			}
			if (empty($absent_section)){
				Helper_Client::alert("请选择原周节次");
			}


			// 班级名称
			$row = $this->model->class->getInfo($class_id);
			$class_name = $row['class_name'];
			// 原学科
			$row = $this->model->subject->getInfo($from_subject);
			$from_subjectname = $row['subject_name'];
			// 现学科
			$row = $this->model->subject->getInfo($to_subject);
			$to_subjectname = $row['subject_name'];
			// 原教师
			$row = $this->model->staff->getInfo($from_staff);
			$from_staffname = $row['staff_name'];
			// 现教师
			$row = $this->model->staff->getInfo($to_staff);
			$to_staffname = $row['staff_name'];

			$absent_itemname = $this->free_ini[$absent_item];

			$week = $this->model->semester->getWeekByTime($start_time);

			$absent_sweek   = $week['week_number'];
			$absent_smonth  = $week['semester_month'];


			if (strpos($absent_section, ",") !== false){
				$section_lists = explode(",", $absent_section);
			}else{
				$section_lists[] = $absent_section;
			}

			foreach ($section_lists as $key => $value){
				$tmp = explode("_", $value);
				$absent_week    = $tmp[0];
				$absent_section = $tmp[1];

				$info = array(
				"semester_id"      => $semester_id,
				"class_id"         => $class_id,
				"class_name"       => $class_name,
				"from_subject"     => $from_subject,
				"from_subjectname" => $from_subjectname,
				"to_subject"       => $to_subject,
				"to_subjectname"   => $to_subjectname,
				"absent_week"      => $absent_week,
				"start_time"       => $start_time,
				"end_time"         => $end_time,
				"absent_smonth"    => $absent_smonth,
				"absent_sweek"     => $absent_sweek,
				"absent_section"   => $absent_section,
				"from_staff"       => $from_staff,
				"from_staffname"   => $from_staffname,
				"to_staff"         => $to_staff,
				"to_staffname"     => $to_staffname,
				"absent_item"      => $absent_item,
				"absent_itemname"  => $absent_itemname,
				"absent_note"      => $absent_note,
				"status"           => 1,
				"create_time"      => date("Y-m-d H:i:s"),
				"update_time"      => "0000-00-00 00:00:00"
				);
				$this->db->do_insert("tbl_absent", $info);

			}
			// 跳转
			Helper_Client::location("添加成功", "index.php?a=absent.list");

		}




		// 教师列表
		$staff_lists = $this->model->staff->getAll();

		// 学科列表
		$subject_lists = $this->model->subject->getAll();



		// 请假原因

		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->assign("subject_lists", $subject_lists);
		$this->tpl->display("absent/absent.add.tpl.php");
	}

	/** 调代课月表 **/
	public function actionStat()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
        $semester_id	= $this->get->semester_id;
        $smonth		= $this->get->month_id;

        $semester_lists = $this->db->fetchAll("SELECT * FROM `tbl_semester`");
        foreach($semester_lists as $key=>$value){
            $semester_lists[$key]['semester_name']=$this->semester_name[$value['semester_name']];
        }
		// 学期
        if(empty($semester_id)){
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];
        }
		// 当前学月
        if(empty($smonth)){
		$week = $this->model->semester->getWeekByTime(date("Y-m-d H:i:s"));
		$smonth = $week['semester_month'];
        }
		$staff      = $this->model->staff->getIni();

		// 周和学月
		$week_lists = $this->db->fetchAll("SELECT * FROM tbl_week WHERE semester_id='{$semester_id}' AND semester_month='{$smonth}'");
		// 总周数
		$week_count = count($week_lists);


		// 课程表配置中的请假原因
		$set_data = $this->model->syllabus->getSet();
		$set_free = $set_data['set_free'];
		foreach ($set_free as $key => $value){
			$free[$value] = $this->free_ini[$value];
		}

		// 学月内的代课
		$absent_lists = $this->db->fetchAll("SELECT * FROM `tbl_absent` WHERE semester_id='{$semester_id}' AND absent_smonth='{$smonth}' ");
		
		// 生成模板
		foreach ($absent_lists as $key => $value){
			foreach ($week_lists as $k => $v){
				foreach ($free as $t => $s){
					$to_staff   = $staff[$value['to_staff']];
					$from_staff = $staff[$value['from_staff']];
					$data_lists[$from_staff]['data'][$v['week_number']]['item'][$s] = "";
					$data_lists[$to_staff]['data'][$v['week_number']]['item'][$s] = "";
					$data_lists[$from_staff]['data'][$value['absent_sweek']]['absent'] = "";
					$data_lists[$to_staff]['data'][$value['absent_sweek']]['absent'] = "";
				}
			}
		}

		// 生成数据
		foreach ($absent_lists as $key => $value){
			foreach ($week_lists as $k => $v){
				$to_staff   = $staff[$value['to_staff']];
				$from_staff = $staff[$value['from_staff']];
				// 请假
				$data_lists[$from_staff]['data'][$value['absent_sweek']]['item'][$free[$value['absent_item']]]++;
				// 代课
				$data_lists[$to_staff]['data'][$value['absent_sweek']]['absent']++;

				// 请假合计
				$data_lists[$from_staff]['item_all']++;

				// 代课合计
				$data_lists[$to_staff]['absent_all']++;
			}

		}
        //$this->tpl->assign("semester_id", $semester_id);
        $this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("free", $free);
		$this->tpl->assign("free_count", count($free)+1);
		$this->tpl->assign("week_lists", $week_lists);
		$this->tpl->assign("data_lists", $data_lists);
		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->display("absent/absent.stat.tpl.php");
	}


	/** 调代汇列表 **/
	public function actionList()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);

        $semester_id	= $this->get->semester_id;
        $month_id		= $this->get->month_id;
        $where1 = "1 = 1";
        $where2 = "1 = 1";
        if(!empty($semester_id)){
            $where1 = " semester_id ='{$semester_id}'";

            $this->tpl->assign("semester_id", $semester_id);
        }
        if (!empty($month_id)) {
            $where2 = " absent_smonth ='{$month_id}'";

        }

        $semester_lists = $this->db->fetchAll("SELECT * FROM `tbl_semester`");
        foreach($semester_lists as $key=>$value){
            $semester_lists[$key]['semester_name']=$this->semester_name[$value['semester_name']];
        }
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_absent` WHERE {$where1} AND {$where2}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$absent_lists = $this->db->fetchAll("SELECT * FROM `tbl_absent` WHERE {$where1} AND {$where2} ORDER BY absent_id DESC LIMIT {$start}, ".PAGE_LIMIT."");


		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
        $this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->assign("absent_lists", $absent_lists);
		$this->tpl->display("absent/absent.list.tpl.php");
	}

}

?>